package his.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import his.entity.CheckApply;
import his.entity.FmedItem;
import his.entity.MedicalRecord;
import his.entity.Register;
import his.persistence.CheckApplyMapper;
import his.persistence.FmedItemMapper;
import his.persistence.MedicalRecordMapper;
import his.persistence.RegisterMapper;
import his.service.CheckApplyService;
import org.example.his.common.api.CommonResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class CheckApplyServiceImpl implements CheckApplyService {

    @Autowired
    CheckApplyMapper checkApplyMapper;

    @Autowired
    RegisterMapper registerMapper;

    @Autowired
    MedicalRecordMapper medicalRecordMapper;

    @Autowired
    FmedItemMapper fmedItemMapper;



    @Override
    public CommonResponse insert_item_result(int medical_id, int regist_id, int item_id, int result_oper_id, String result) {

        QueryWrapper<CheckApply> queryWrapper1=new QueryWrapper<>();
        queryWrapper1.eq("MedicalID",medical_id);
        queryWrapper1.eq("RegistID",regist_id);
        queryWrapper1.eq("ItemID",item_id);
        queryWrapper1.eq("State",4);
        List<CheckApply> checkApplyList=checkApplyMapper.selectList(queryWrapper1);
        if(checkApplyList.size()>0){
            QueryWrapper<CheckApply> queryWrapper2=new QueryWrapper<>();
            queryWrapper2.eq("MedicalID",medical_id);
            queryWrapper2.eq("RegistID",regist_id);
            queryWrapper2.eq("ItemID",item_id);
            queryWrapper2.eq("State",4);
            CheckApply checkApply=new CheckApply();
            checkApply.setResultOperID(result_oper_id);
            checkApply.setResult(result);
            Date date=new Date();
            checkApply.setResultTime(date);
            checkApply.setState(5);
            int result1=checkApplyMapper.update(checkApply,queryWrapper2);
            return CommonResponse.createForSuccess("项目结果录入完成");
        }else{
            return CommonResponse.createForError("未找到该项目申请单");
        }

    }

    @Override
    public CommonResponse excute_item(int medical_id, int regist_id, int item_id, int checkOperID) {

        QueryWrapper<CheckApply> queryWrapper1=new QueryWrapper<>();
        queryWrapper1.eq("MedicalID",medical_id);
        queryWrapper1.eq("RegistID",regist_id);
        queryWrapper1.eq("ItemID",item_id);
        queryWrapper1.eq("State",3);
        List<CheckApply> checkApplyList=checkApplyMapper.selectList(queryWrapper1);
        if(checkApplyList.size()>0){
            QueryWrapper<CheckApply> queryWrapper2=new QueryWrapper<>();
            queryWrapper2.eq("MedicalID",medical_id);
            queryWrapper2.eq("RegistID",regist_id);
            queryWrapper2.eq("ItemID",item_id);
            queryWrapper2.eq("State",3);
            CheckApply checkApply=new CheckApply();
            checkApply.setState(4);
            checkApply.setMedicalID(medical_id);
            checkApply.setRegistID(regist_id);
            checkApply.setItemID(item_id);
            Date date=new Date();
            checkApply.setCheckTime(date);
            checkApply.setCheckOperID(checkOperID);
            int result=checkApplyMapper.update(checkApply,queryWrapper2);
            return CommonResponse.createForSuccess("项目执行完成");
        }else{
            return CommonResponse.createForError("未找到该项目申请单");
        }

    }

    @Override
    public CommonResponse check_item(int medical_id, int regist_id, int item_id, String name, String objective, String position, int is_urgent, int num, int doctor_id, int record_type) {

        QueryWrapper<CheckApply> queryWrapper1=new QueryWrapper<>();
        queryWrapper1.eq("MedicalID",medical_id);
        queryWrapper1.eq("RegistID",regist_id);
        queryWrapper1.eq("ItemID",item_id);
        List<CheckApply> checkApplyList=checkApplyMapper.selectList(queryWrapper1);
        if(checkApplyList.size()>0){
            return CommonResponse.createForError("新增失败，已开申请单");
        }else{
            QueryWrapper<FmedItem> queryWrapper2=new QueryWrapper<>();
            queryWrapper2.eq("ID",item_id);
            FmedItem fmedItem=fmedItemMapper.selectOne(queryWrapper2);
            if(fmedItem!=null){

                CheckApply checkApply=new CheckApply();
                checkApply.setMedicalID(medical_id);
                checkApply.setRegistID(regist_id);
                checkApply.setItemID(item_id);
                checkApply.setName(name);
                checkApply.setObjective(objective);
                checkApply.setPosition(position);
                checkApply.setIsUrgent(is_urgent);
                checkApply.setNum(num);
                checkApply.setRecordType(fmedItem.getRecordType());
                checkApply.setDoctorID(doctor_id);
                checkApply.setRecordType(record_type);
                Date creationTime=new Date();
                checkApply.setCreationTime(creationTime);
                checkApply.setState(2);
                checkApplyMapper.insert(checkApply);
                return CommonResponse.createForSuccess("新增成功");
            }else{
                return CommonResponse.createForError("没有该项目");
            }

        }


    }

    @Override
    public CommonResponse<List<CheckApply>> get_patient_item(String keywords) {

        QueryWrapper<Register> queryWrapper1=new QueryWrapper<>();
        queryWrapper1.eq("RealName",keywords);
        List<Register> registerList1=registerMapper.selectList(queryWrapper1);
        System.out.println(registerList1.size());
        if(registerList1.size()>0){
            int id=-1;
            for(int i=0;i<registerList1.size();i++){
                QueryWrapper<MedicalRecord> queryWrapper2=new QueryWrapper<>();
                int registid=registerList1.get(i).getID();
                System.out.println(registid);
                queryWrapper2.eq("RegistID",registid);

                MedicalRecord medicalRecord=medicalRecordMapper.selectOne(queryWrapper2);
                if(medicalRecord!=null){
                    id=medicalRecord.getID();
                }

            }
            System.out.println(id);
            if(id!=-1){
                QueryWrapper<CheckApply> queryWrapper3=new QueryWrapper<>();
                queryWrapper3.eq("MedicalID",id);
                queryWrapper3.ne("State",5);
                List<CheckApply> checkApplyList1=checkApplyMapper.selectList(queryWrapper3);
                if(checkApplyList1.size()>0){
                    return CommonResponse.createForSuccess(checkApplyList1);
                }else{
                    return CommonResponse.createForError("无项目");
                }

            }else{
                return CommonResponse.createForError("无项目");
            }
        }else{
            QueryWrapper<CheckApply> queryWrapper4=new QueryWrapper<>();
            queryWrapper4.eq("MedicalID",keywords);
            queryWrapper4.lt("State",5).gt("State",2);
            List<CheckApply> checkApplyList2=checkApplyMapper.selectList(queryWrapper4);
            return CommonResponse.createForSuccess(checkApplyList2);
        }


    }
}
